/*
 * Copyright © Yan Zhenjie. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iotimc.attendancesystem.server.response;

import android.text.TextUtils;
import android.util.Log;

import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.dao.SystemEntityDao;
import com.iotimc.attendancesystem.entity.SystemEntity;
import com.iotimc.attendancesystem.event.NoticeEvent;
import com.yanzhenjie.andserver.RequestHandler;
import com.yanzhenjie.andserver.util.HttpRequestParser;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Created by cwj
 */
public class RequestSystemHandler implements RequestHandler {
    private static final String TAG = "RequestSystemHandler";

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {

        Map<String, String> params = HttpRequestParser.parse(request);
        Log.i(TAG, "handle: " + params.toString());


        SystemEntityDao systemEntityDao = GreenDaoManager.getInstance().getSession().getSystemEntityDao();
        SystemEntity load = systemEntityDao.load(1L);
        if (load == null) {
            load = new SystemEntity();
            setEntity(load, params);
            systemEntityDao.insert(load);
        } else {
            setEntity(load, params);
            systemEntityDao.update(load);
        }

        EventBus.getDefault().post(new NoticeEvent(true));
        StringEntity stringEntity = new StringEntity("保存成功", "utf-8");
        response.setEntity(stringEntity);

    }

    private void setEntity(SystemEntity entity, Map<String, String> params) {
        String content = params.get("content");
        String img64 = params.get("img64");
        String title = params.get("title");
        String message = params.get("message");

        if (!TextUtils.isEmpty(content)) {
            String decode = decode(content);
            entity.setContent(decode);
        }

        if (!TextUtils.isEmpty(img64)) {
            String decode = decode(img64);
            img64 = decode.substring(decode.indexOf(',') + 1);
            Log.i(TAG, "setEntity: " + decode);

            entity.setImg64(img64);
        }

        if (!TextUtils.isEmpty(title)) {
            String decode = decode(title);
            entity.setTitle(decode);
        }

        if (!TextUtils.isEmpty(message)) {
            String decode = decode(message);
            entity.setNotice(decode);
        }

    }

    private String decode(String info) {
        String decode = "";
        try {
            decode = URLDecoder.decode(info, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return decode;
    }


}
